<?php

namespace app\home\controller;

use think\Controller;
use think\Db;
use think\cache\driver\Redis;
/**
 * 基础控制器
 * Class Base
 * @package app\home\controller
 */
class Base extends Controller
{
    /**
     * page当前页码
     * @var string
     */
    public $page = '';
    /**
     * 每页显示多少条
     * @var string
     */
    public $size = '';
    /**
     * 查询条件的起始值
     * @var int
     */
    public $from = 0;
    /**
     * 用户信息
     * @var int
     */
    public $user = array();
    /**
     * 路由
     * @var int
     */
    public $url = '';
    /**
     * 实例化Redis
     * @var int
     */
    public $redis;

    public function _initialize()
    {
        $param = input('param.');
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = $module."/".$controller."/".$action;
        $this->url = $url;
        if(!in_array($url, config('home.permission_authlist')))
        {
            if(empty($param['token'])){
                json(array("code"=>"1001","msg"=>"非法访问"))->send();die;
            }else{
                $mem = Db::name('member')->where('token',$param['token'])->find();
                $this->user = $mem;
                if(empty($mem)){
                    json(array("code"=>"1002","msg"=>"token错误"))->send();die;
                }else{
                    if($mem['thawingtime'] === 0){
                        json(array("code"=>"1002","msg"=>"您的账号已被永久封禁"))->send();die;
                    }

                    if($mem['thawingtime'] > time()){
                        json(array("code"=>"1002","msg"=>"您的账号已被封禁，".'解封日期为'.date('Y-m-d H:i:s',$mem['thawingtime'])))->send();die;
                    }
                    if(time() - $mem['tokentime'] > config('home.token_expire')){
                        json(array("code"=>"1002","msg"=>"token过期"))->send();die;
                    }
                    $this->redis = new Redis();
                    if(empty($mem['qrcode'])){
                        $qrcode = Qrcode($param['token'],'https://wx.fushan8.com/#/invitePage?id='.$mem['id']);
                        Db::name('member')->where('id',$mem['id'])->update(['qrcode'=>$qrcode,'tokentime'=>time()]);
                    }else{
                        Db::name('member')->where('id',$mem['id'])->update(['tokentime'=>time()]);
                    }
                }
            }
        }else{
            if(!empty($param['token'])){
                $mem = Db::name('member')->where('token',$param['token'])->find();
                $this->user = $mem;
            }
        }
        if($url == 'home/banner/getindex'){
            if(!empty($mem['img'])){
                $oss = $this->douserheadimg($mem['img'],$mem['id']);
                if($oss != 1){
                    Db::name('member')->where('id',$mem['id'])->update(['img'=>$oss]);
                    $result = 'F'.substr($mem['img'],strripos($mem['img'],"FS")+1);
                    ossdel($result);
                }
            }
        }
    }
    /**
     * [getMember  获取用户数据]
     * @author [C。]
     * @param $data
     */
    public function douserheadimg($headimg,$id) {
        if(strpos($headimg,'fushan.oss-cn-hangzhou.aliyuncs.com/') == false){
            if(strpos($headimg,'Uploads/kimg/') != false){
                $filename = getlocalimg('http://www.fushan8.com'.$headimg);
                $name = uploadoss($filename,'',$id);
                return $name;
            }
            if(strpos($headimg,'Public/nouser.png') != false){
                return config('home.no_user_headimg');
            }
            if(strpos($headimg,'thirdwx.qlogo.cn') != false){
                $filename = getlocalimg($headimg);
                $name = uploadoss($filename,'',$id);
                return $name;
            }
        }else{
            return 1;
        }
    }
    /**
     * [getMember  获取用户数据]
     * @author [C。]
     * @param $data
     */
    public function getMember($data) {
        $mem = Db::name('member')->where('token',$data)->find();
        return $mem;
    }
    /**
     * [getPageAndSize  获取分页page size 内容]
     * @author [C。]
     * @param $data
     */
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty(config('list_rows')) ? config('list_rows') : config('paginate.list_rows');//后台设置显示条数、配置文件显示条数
        $this->from = ($this->page - 1) * $this->size;
    }
}